<template>
  <div>
    <el-table :data="tableData"
              border
              style="width: 100%">
      <el-table-column prop="user.account"
                       label="用户"
                       width="180">
      </el-table-column>
      <el-table-column prop="user.sex"
                       label="性别"
                       width="100">
      </el-table-column>
      <el-table-column prop="user.department"
                       label="部门"
                       width="180">
      </el-table-column>
      <el-table-column prop="user.telphone"
                       label="联系方式"
                       width="180">
      </el-table-column>
      <el-table-column prop="user.email"
                       label="邮箱"
                       width="180">
      </el-table-column>
      <el-table-column prop="role"
                       label="角色"
                       width="150">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini"
                     type="danger"
                     @click="deletPrem(scope.$index, scope.row)">注销用户</el-button>
          <el-popover placement="left"
                      width="400"
                      trigger="click">
            <el-table :data="Role">
              <el-table-column width="80"
                               prop="role"
                               label="角色名"></el-table-column>
              <el-table-column width="150"
                               prop="description"
                               label="角色描述"></el-table-column>
              <el-table-column width="100"
                               label="操作">
                <template slot-scope="scope1">
                  <el-button size="mini"
                             @click="onRowClick(scope1.row)">确定</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-button slot="reference"
                       @click="set(scope.$index,scope.row)">设置角色</el-button>
          </el-popover>
          <el-popover placement="left"
                      width="400"
                      trigger="click">
            <el-table :data="Perm">
              <el-table-column width="80"
                               prop="perm"
                               label="角色名"></el-table-column>
              <el-table-column width="180"
                               prop="description"
                               label="角色描述"></el-table-column>
            </el-table>
            <el-button slot="reference"
                       @click="get(scope.$index,scope.row)">查看权限</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>

  </div>
</template>

<script>
export default {
  data () {
    return {
      tableData: [],
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: '添加',
      editForm: {
        perm: "",
        description: "",
        flag: "",
        id: ""
      },
      Role: [],
      Perm: []
    }
  },
  created () {
    this.getdata()
  },
  inject: ["refresh"],
  methods: {
    getdata () {
      this.$axios.get('/api/users/findAllUser').then(res => {
        if (res.data.code == 200) {
          this.tableData = res.data.data;

        }
      })
    },
    deletPrem (index, row) {

      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$axios.get("/api/users/delect/" + row.user.id)
            .then(res => {
              if (res.data.code == 200) {
                this.refresh()
              } else {
              }
            })
            .catch(err => {
              this.loading = false
              this.$message.error('系统维护中')
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    set (index, row) {
      console.log(row.user);
      this.$axios.get("/api/role/findAll").then(res => {
        if (res.data.code == 200) {
          localStorage.setItem("userid", row.user.id)
          this.Role = res.data.data;
        }
      })
    },
    get (index, row) {
      console.log("999");
      this.$axios.get("/api/role/findUserRole/" + row.user.id).then(res => {
        if (res.data.code == 200) {
          localStorage.setItem("userid", row.user.id)
          this.Perm = res.data.data;
          console.log(this.Perm);
        }
      })
    },
    onRowClick (row) {

      console.log(row);
      this.$axios.get("/api/users/setRole/" + row.id + "/" + localStorage.getItem("userid")).then(res => {
        if (res.data.code == 200) {
          this.refresh()
        }
      })
    },
    closeDialog () {
      this.editFormVisible = false
    }
  }
}
</script>